<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#assign currentMenu="productDown"/>
    <#include "../common/header.ftl">
    <script src="/js/plugins/jquery-bootstrap/jquery.bootstrap.min.js"></script>
    <script src="/js/ckeditor/ckeditor.js"></script>
    <style type="text/css">
        .modal-body {
            max-height: 750px;
            overflow-y: auto;
        }
        .modal-body img {
            width: 100%;
        }
    </style>
</head>
<body>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-lg-3">
            <#include "../common/menu.ftl">
        </div>
        <div class="col-lg-9">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-head-line">所有商品管理</h1>
                </div>
            </div>
            <#--高级查询--->
            <form class="form-inline" id="searchForm" action="/product/productDownList.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <div class="form-group">
                    <label>关键字:</label>
                    <input type="text" class="form-control" name="keyword" value="${(qo.keyword)!}"
                           placeholder="请输入商品名称">
                </div>

                <button id="btn_query" class="btn btn-info">
                    <span class="glyphicon glyphicon-search"></span> 查询
                </button>

            </form>

            <table class="table table-striped table-hover">
                <tr>
                    <th>序号</th>
                    <th>商品名称</th>
                    <th>商品简介</th>
                    <th>商品图片</th>
                    <th>商品状态</th>
                    <th>商品价格</th>
                    <th>商品库存</th>
                    <th>添加时间</th>
                    <th>操作</th>
                </tr>
                <#list pageInfo.list as entity>
                    <tr>
                        <td>${entity_index + 1}</td>
                        <td>${(entity.productName)!}</td>
                        <td>${(entity.simpleInfo)!}</td>
                        <td><img src="${(entity.productImg)!}" width="70px"/></td>
                        <td>${entity.stateName}</td>
                        <td>${(entity.price)!}</td>
                        <td>${(entity.stock)!}</td>
                        <td>${entity.addTime?string('yyyy-MM-dd HH:mm:ss')}</td>
                        <td>
                                <a role="button" class="btn btn-success btn-xs changeStateBtn" data-json='{"id": ${entity.id}, "state": 1}'>
                                    <span class="glyphicon glyphicon-ok"></span> 重新上架
                                </a>
                                <a role="button" class="btn btn-danger btn-xs inputBtn" data-json='${entity.json}'>
                                    <span class="glyphicon glyphicon-pencil"></span> 修改
                                </a>
                            <a role="button" class="btn btn-primary btn-xs InfoBtn" data-tid="${entity.id}">
                                <span class="glyphicon glyphicon-sunglasses"></span> 查看商品详情
                            </a>
                        </td>
                    </tr>
                </#list>
            </table>
            <#include "../common/page.ftl">
        </div>
    </div>
</div>
<#--商品内容模态窗-->
<div id="contentModal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">商品内容</h4>
            </div>
            <div class="modal-body">
            </div>
            <div class="modal-footer">
                <a role="button" class="btn btn-info" data-dismiss="modal" aria-hidden="true">关闭</a>
            </div>
        </div>
    </div>
</div>

<#--商品添加与编辑模态窗-->
<div id="inputModal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/product/saveOrUpdate.do" method="post"
                      enctype="multipart/form-data" id="editForm">
                    <input type="hidden" name="id"/>
                    <div class="form-group" >
                        <label class="col-lg-4 control-label">商品名称：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="productName"/>
                        </div>
                    </div>
                    <div class="form-group" >
                        <label class="col-lg-4 control-label">商品简介：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="simpleInfo"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">商品图片：</label>
                        <div class="col-lg-6">
                            <img id="productImg" style="width: 100%"/>
                            <input type="file" class="form-control" name="img"/>
                        </div>
                    </div>


                    <div class="form-group">
                        <label class="col-lg-4 control-label">商品状态：</label>
                        <div class="col-lg-6">
                            <select class="form-control" name="state">
                                <option value="1">上架</option>
                                <option value="0">下架</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-group" >
                        <label class="col-lg-4 control-label">商品价格：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="price"/>
                        </div>
                    </div>
                    <div class="form-group" >
                        <label class="col-lg-4 control-label">商品库存：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="stock"/>
                        </div>
                    </div>


                    <div class="form-group">
                        <textarea name="productInfo.productInfo" id="ckeditor"></textarea>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary submitBtn">保存</button>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        //找到内容的按钮,绑定点击事件
        $(".InfoBtn").click(function () {
            var id = $(this).data("tid");
            //1:发送ajax请求,拿到当前游记的内容
            $.get("/product/getInfo.do", {id: id}, function (data) {
                //2:回显到模态框正文部分
                $("#contentModal .modal-body").html(data);
                //3:显示模态框
                $("#contentModal").modal("show");
            });
        });

        //重新上架的功能
        $(".changeStateBtn").click(function () {
            var json = $(this).data("json");
            console.log(json);
            $.messager.confirm("温馨提示", "你确认要重新上架吗?", function () {
                //发送ajax请求,执行更新状态
                $.post("/product/changeState.do", json, function (data) {
                    location.reload();
                });
            });
        });

        //初始化一个CKeditor
        var edt = CKEDITOR.replace("ckeditor");
        //点击修改或新增按钮
        $(".inputBtn").click(function () {
            //清空模态窗内容
            $("#editForm input").val("");
            $("#editForm select[name='state']").val(1);

            $("#productImg").attr("src", "");
            //处理参数回显
            var json = $(this).data("json");
            if (json){
                $("#editForm input[name='id']").val(json.id);
                $("#editForm input[name='productName']").val(json.productName);
                $("#editForm input[name='simpleInfo']").val(json.simpleInfo);
                $("#productImg").attr("src", json.productImg);
                $("#editForm select[name='state']").val(json.state);
                $("#editForm input[name='price']").val(json.price);
                $("#editForm input[name='stock']").val(json.stock);
               // $("#editForm input[name='addTime']").val(json.addTime);
                console.log(json.addTime);

                $.get("/product/getInfoById.do", {id: json.id}, function (data) {
                    //文章的内容回显到ckeditor
                    edt.setData(data);
                }, "html");

            }
            //点击保存,提交表单
            $(".submitBtn").click(function () {
                //把CKEditor中的数据填充到文本域中,再提交表单
                $("#ckeditor").html(edt.getData());
                $("#editForm").submit();
            })

            $("#inputModal").modal("show");
        })

    });
</script>
</body>
</html>